﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Blog.Helper;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Core;
using MvcEngine.Blog.Components.Avatars;
using MvcEngine.Blog.Managers;
using MvcEngine.Blog.Models;

namespace MvcEngine.Blog.Filters
{
    public class PostDetailsFilter : IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            BlogPostDetailsViewModel model = filterContext.Controller.ViewData.Model as BlogPostDetailsViewModel;
            if (model == null)
                return;

            BlogData blog = BlogManager.Current.GetCurrentBlog();
            model.IsCommentEnabled = blog.AllowComments & model.IsCommentEnabled;

            Dictionary<string, string> avatars = new Dictionary<string, string>();
            foreach (var item in model.Comments)
            {
                if (!avatars.ContainsKey(item.Email))
                    avatars[item.Email] = IoC.Resolve<IAvatarProvider>().GetUrl(item.Email);
            }
            model.SetModelValue("Avatars", avatars);
        }
    }
}
